package com.example.demo.controller;

import com.example.demo.model.Material;
import com.example.demo.model.vo.MaterialAddVo;
import com.example.demo.service.SpringBootService;
import com.example.demo.util.ConfigInfo;
import com.example.demo.util.Result;
import com.example.demo.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by 86151 on 2018/12/24.
 */
@RestController
public class SpringBootController {
//    @GetMapping
//    @PostMapping

//@Value通过此注解获取到配置文件中的指定信息
   /* @Value("${name}")
    private String name;
    @Value("${location}")
    private String location;*/

    @Autowired
    private ConfigInfo configInfo;

    @Autowired
    private SpringBootService springBootService;


    @GetMapping("/test")
    public List<Material> test(){
        return springBootService.queryAll();
    }
    @PostMapping("/batchQueryAllByIds")
    public List<Material> batchQueryAllByIds(@RequestBody List<Integer> ids){
        return springBootService.batchQueryAllByIds(ids);
    }

    @PostMapping("/batchAddMaterial")
    public List<Material> batchAddMaterial(@RequestBody List<Material> material){
        return springBootService.batchAddMaterial(material);
    }


    @PostMapping("/batchUpdateMaterial")
    public List<Material> batchUpdateMaterial(@RequestBody List<Material> material){
        return springBootService.batchUpdateMaterial(material);
    }


    /**
     *
        对应的是postman 写入{"id":1,"name":"小明"}
     */
        @PostMapping("/test1")
    public List<Material> test1(@RequestBody Material material){
        return null;
    }
        /**
         *
         对应的是postman 写入{"111","222"}
         */
    @PostMapping("/test2")
    public List<Material> test2(@RequestBody List<String> test2){
        return null;
    }
        /**
         *
         对应的是postman 写入[{"111","222"}，{"111","222"}]
         */
    @PostMapping("/test3")
    public List<Material> test3(@RequestBody List<Material> test3){
        return null;
    }



    @PostMapping("addMaterial")
    public Result<MaterialAddVo> addMaterial(@RequestBody @Valid MaterialAddVo materialAddVo, BindingResult bindingResult)throws Exception{
        if (bindingResult.hasErrors()){
            return ResultVo.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        return springBootService.addMaterial(materialAddVo);
    }

}

